﻿namespace BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDDMNhaCungCap
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private int _deid;
        private int _istt;
        private string _sdaidien;
        private string _sdiachi;
        private string _sdienthoai;
        private string _sma;
        private string _smasothue;
        private string _snganhang;
        private string _sSchema;
        private string _ssotk;
        private string _sten;

        public dtoDDMNhaCungCap()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDDMNhaCungCap(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDDMNhaCungCap.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand("delete from " + this._sSchema + "." + tableName + " where 1=1 " + ((this._deid == 0) ? "" : string.Concat(new object[] { " and ", columnID, " = ", this._deid })), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDDMNhaCungCap.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDDMNhaCungCap.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = Convert.ToInt32(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDDMNhaCungCap.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDDMNhaCungCap.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnSTT, ",", columnMA, ",", columnTEN, ",", columnDIACHI, ",", columnDIENTHOAI, 
                    ",", columnSOTK, ",", columnMASOTHUE, ",", columnNGANHANG, ",", columnDAIDIEN, ") values(", this._deid, ",", this._istt, ",'", this._sma, "',:", columnTEN, 
                    ",:", columnDIACHI, ",'", this._sdienthoai, "','", this._ssotk, "','", this._smasothue, "',:", columnNGANHANG, ",:", columnDAIDIEN, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTEN, NpgsqlDbType.Text, 200).Value = this._sten;
                command.Parameters.Add(columnDIACHI, NpgsqlDbType.Text, 200).Value = this._sdiachi;
                command.Parameters.Add(columnNGANHANG, NpgsqlDbType.Text, 200).Value = this._snganhang;
                command.Parameters.Add(columnDAIDIEN, NpgsqlDbType.Text, 200).Value = this._sdaidien;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDDMNhaCungCap.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0;
            this._istt = 0;
            this._sma = "";
            this._sten = "";
            this._sdiachi = "";
            this._sdienthoai = "";
            this._ssotk = "";
            this._smasothue = "";
            this._snganhang = "";
            this._sdaidien = "";
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDDMNhaCungCap.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnSTT, ",", columnMA, ",", columnTEN, ",", columnDIACHI, ",", columnDIENTHOAI, ",", columnSOTK, ",", columnMASOTHUE, 
                    ",", columnNGANHANG, ",", columnDAIDIEN, " from ", this._sSchema, ".", tableName, " where ", columnID, " = ", this._deid
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = Convert.ToInt32(dataTable.Rows[0][columnID].ToString());
                    this._istt = Convert.ToInt32(dataTable.Rows[0][columnSTT].ToString());
                    this._sma = dataTable.Rows[0][columnMA].ToString();
                    this._sten = dataTable.Rows[0][columnTEN].ToString();
                    this._sdiachi = dataTable.Rows[0][columnDIACHI].ToString();
                    this._sdienthoai = dataTable.Rows[0][columnDIENTHOAI].ToString();
                    this._ssotk = dataTable.Rows[0][columnSOTK].ToString();
                    this._smasothue = dataTable.Rows[0][columnMASOTHUE].ToString();
                    this._snganhang = dataTable.Rows[0][columnNGANHANG].ToString();
                    this._sdaidien = dataTable.Rows[0][columnDAIDIEN].ToString();
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDDMNhaCungCap.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDDMNhaCungCap.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnSTT + "," + columnMA + "," + columnTEN + "," + columnDIACHI + "," + columnDIENTHOAI + "," + columnSOTK + "," + columnMASOTHUE + "," + columnNGANHANG + "," + columnDAIDIEN + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDDMNhaCungCap.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDDMNhaCungCap.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnSTT, " = ", this._istt, ",", columnMA, " = '", this._sma, "',", columnTEN, " = :", columnTEN, 
                    ",", columnDIACHI, " = :", columnDIACHI, ",", columnDIENTHOAI, " = '", this._sdienthoai, "',", columnSOTK, " = '", this._ssotk, "',", columnMASOTHUE, " = '", this._smasothue, 
                    "',", columnNGANHANG, " = :", columnNGANHANG, ",", columnDAIDIEN, " = :", columnDAIDIEN, " where ", columnID, " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTEN, NpgsqlDbType.Text, 200).Value = this._sten;
                command.Parameters.Add(columnDIACHI, NpgsqlDbType.Text, 200).Value = this._sdiachi;
                command.Parameters.Add(columnNGANHANG, NpgsqlDbType.Text, 200).Value = this._snganhang;
                command.Parameters.Add(columnDAIDIEN, NpgsqlDbType.Text, 200).Value = this._sdaidien;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDDMNhaCungCap.cs");
                return false;
            }
        }

        public static string columnDAIDIEN
        {
            get
            {
                return "daidien";
            }
        }

        public static string columnDIACHI
        {
            get
            {
                return "diachi";
            }
        }

        public static string columnDIENTHOAI
        {
            get
            {
                return "dienthoai";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnMA
        {
            get
            {
                return "ma";
            }
        }

        public static string columnMASOTHUE
        {
            get
            {
                return "masothue";
            }
        }

        public static string columnNGANHANG
        {
            get
            {
                return "nganhang";
            }
        }

        public static string columnSOTK
        {
            get
            {
                return "sotk";
            }
        }

        public static string columnSTT
        {
            get
            {
                return "stt";
            }
        }

        public static string columnTEN
        {
            get
            {
                return "ten";
            }
        }

        public string pDAIDIEN
        {
            get
            {
                return this._sdaidien;
            }
            set
            {
                this._sdaidien = value;
            }
        }

        public string pDIACHI
        {
            get
            {
                return this._sdiachi;
            }
            set
            {
                this._sdiachi = value;
            }
        }

        public string pDIENTHOAI
        {
            get
            {
                return this._sdienthoai;
            }
            set
            {
                this._sdienthoai = value;
            }
        }

        public int pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public string pMA
        {
            get
            {
                return this._sma;
            }
            set
            {
                this._sma = value;
            }
        }

        public string pMASOTHUE
        {
            get
            {
                return this._smasothue;
            }
            set
            {
                this._smasothue = value;
            }
        }

        public string pNGANHANG
        {
            get
            {
                return this._snganhang;
            }
            set
            {
                this._snganhang = value;
            }
        }

        public string pSOTK
        {
            get
            {
                return this._ssotk;
            }
            set
            {
                this._ssotk = value;
            }
        }

        public int pSTT
        {
            get
            {
                return this._istt;
            }
            set
            {
                this._istt = value;
            }
        }

        public string pTEN
        {
            get
            {
                return this._sten;
            }
            set
            {
                this._sten = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_dmnhacungcap";
            }
        }
    }
}

